<template>
  <div class="register-container">
    <!-- 注册表单区域 -->
    <div class="register-box">
      <h2 class="register-subtitle">用户注册</h2>
      
      <a-form
        :model="registerForm"
        :rules="rules"
        @finish="handleRegister"
        layout="vertical"
      >
        <a-form-item label="手机号" name="phone">
          <a-input
            v-model:value="registerForm.phone"
            placeholder="请输入手机号"
            size="large"
          >
            <template #prefix>
              <MobileOutlined />
            </template>
          </a-input>
        </a-form-item>

        <a-form-item label="验证码" name="code">
          <a-input
            v-model:value="registerForm.code"
            placeholder="请输入验证码"
            size="large"
          >
            <template #prefix>
              <MailOutlined />
            </template>
            <template #suffix>
              <a-button
                type="link"
                :disabled="countdown > 0"
                @click="handleSendCode"
              >
                {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
              </a-button>
            </template>
          </a-input>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            block
            :loading="loading"
          >
            注册
          </a-button>
        </a-form-item>
      </a-form>

      <div class="register-footer">
        <a-button type="link" @click="goToLogin">
          已有账号？立即登录
        </a-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { MobileOutlined, MailOutlined } from '@ant-design/icons-vue'
import { register, sendVerifyCode } from '../api/auth'

const router = useRouter()
const loading = ref(false)
const countdown = ref(0)

// 注册表单
const registerForm = reactive({
  phone: '',
  code: ''
})

// 验证规则
const rules = {
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

// 发送验证码
const handleSendCode = async () => {
  if (!registerForm.phone) {
    message.warning('请先输入手机号')
    return
  }
  
  if (!/^1[3-9]\d{9}$/.test(registerForm.phone)) {
    message.warning('请输入正确的手机号')
    return
  }

  try {
    await sendVerifyCode(registerForm.phone)
    message.success('验证码已发送')
    
    // 开始倒计时
    countdown.value = 60
    const timer = setInterval(() => {
      countdown.value--
      if (countdown.value <= 0) {
        clearInterval(timer)
      }
    }, 1000)
  } catch (error) {
    message.error(error.response?.data?.message || '发送验证码失败')
  }
}

// 注册
const handleRegister = async () => {
  loading.value = true
  try {
    await register(registerForm)
    message.success('注册成功，请登录')
    router.push('/login')
  } catch (error) {
    message.error(error.response?.data?.message || '注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 返回登录
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url('/src/assets/image/shamo.png') no-repeat center center;
  background-size: cover;
  position: relative;
}

.register-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 0;
}



/* 注册表单区域 */
.register-box {
  width: 450px;
  padding: 45px 50px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  position: relative;
  z-index: 10;
}

.register-subtitle {
  text-align: center;
  margin-bottom: 35px;
  font-size: 26px;
  font-weight: 600;
  color: #003366;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
}

.register-footer {
  margin-top: 25px;
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.register-footer :deep(.ant-btn-link) {
  color: #1890ff;
  font-size: 14px;
}

.register-footer :deep(.ant-btn-link:hover) {
  color: #40a9ff;
}

/* 表单项样式优化 */
:deep(.ant-form-item-label > label) {
  font-weight: 500;
  color: #333;
}

:deep(.ant-input-affix-wrapper),
:deep(.ant-input) {
  border-radius: 6px;
}

:deep(.ant-btn-primary) {
  height: 44px;
  font-size: 16px;
  border-radius: 6px;
  background: linear-gradient(135deg, #003366 0%, #004d99 100%);
  border-color: #003366;
  box-shadow: 0 4px 12px rgba(0, 51, 102, 0.3);
  transition: all 0.3s ease;
}

:deep(.ant-btn-primary:hover) {
  background: linear-gradient(135deg, #004d99 0%, #0066cc 100%);
  border-color: #004d99;
  box-shadow: 0 6px 16px rgba(0, 51, 102, 0.4);
  transform: translateY(-2px);
}
</style>
